package com.qianzhu8.kfc.boot.util;

import lombok.experimental.UtilityClass;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * BeanUtils
 *
 * @author xuyanjun
 * @date 2020-04-12
 */
@UtilityClass
public class BeanUtils {

    /**
     * 获取对象及其所有父类的所有字段
     *
     * @return 所有字段
     */
    public static List<Field> getAllFields(Class clazz) {
        List<Field> fields = new ArrayList<>();

        while (clazz != null) {
            Field[] declaredFields = clazz.getDeclaredFields();
            if (declaredFields.length > 0) {
                fields.addAll(Arrays.asList(declaredFields));
            }

            clazz = clazz.getSuperclass();
        }

        return fields;
    }
}
